<template>
  <div class="proOrderDetails" @click.stop="aotuTab=false">
    <div style="margin-bottom: 10px;display: flex">
      <el-button size="mini" type="primary" @click="save">保存</el-button>
    </div>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="production" label-width="100px" size="mini">
          <el-form-item :label="$t('market.orderDate')">
            <el-date-picker
              v-model.trim="production.fdate"
              type="date"
              size="mini"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item :label="$t('market.consignDate')">
            <el-date-picker
              v-model.trim="production.fdeliverydate"
              type="date"
              size="mini"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item :label="$t('market.workshop')">
            <el-input v-model.trim="production.fworkshop">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleWork(0, 1)"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.paezText1')">
            <el-input v-model.trim="production.salKhddh" />
          </el-form-item>
          <el-form-item :label="$t('market.cust')">
            <el-input v-model.trim="production.fcustomer" />
          </el-form-item>
          <el-form-item :label="$t('market.descrip')">
            <el-input v-model.trim="production.fdescription" />
          </el-form-item>
        </el-form>
        <div style="margin-top: 10px;">
          <el-button size="mini" style="margin-bottom: 5px;" @click="newHang">{{ $t('btn.newHang') }}<!--新增行--></el-button>
          <el-button size="mini" style="margin-bottom: 5px;" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
        </div>
        <jc-table
          ref="table"
          :table-data="tableData"
          :table-header="tableHeard"
          :table-height="tableHeight"
          serial
          @rowClick="cellClick"
        >
          <!--  物料编码  -->
          <template v-slot:billSlot="col">
            <el-input
              v-model.trim="col.scope.row.fnumber"
              size="mini"
            />
          </template>
          <template v-slot:text2Slot="col">
            <el-input v-model="col.scope.row.fmatname" size="mini" />
          </template>
          <template v-slot:text3Slot="col">
            <el-input v-model="col.scope.row.fmatmodel" size="mini" />
          </template>
          <template v-slot:text4Slot="col">
            <el-input v-model="col.scope.row.fmatspec" size="mini" />
          </template>
          <template v-slot:text5Slot="col">
            <el-input v-model="col.scope.row.fmatunit" size="mini" />
          </template>
          <template v-slot:text6Slot="col">
            <el-input v-model="col.scope.row.fmatdesc" size="mini" />
          </template>
          <!--  数量  -->
          <template v-slot:tagSlot="col">
            <el-input-number v-model="col.scope.row.fqty" size="mini" />
          </template>
          <!--要货日期-->
          <template v-slot:btnState="col">
            <el-date-picker
              v-model="col.scope.row.fdeliverydate"
              type="date"
              size="mini"
              value-format="yyyy-MM-dd"
            />
          </template>
          <!--生产车间-->
          <template v-slot:btnStates="col">
            <el-input v-model.trim="col.scope.row.fdept">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleWork(col.scope.$index, 2)"
              />
            </el-input>
          </template>
        </jc-table>
      </el-tab-pane>
    </el-tabs>
    <!--    物料弹窗-->
    <material v-if="isMateria" :msg="material" @material="materialData" />
    <!--  生产部门-->
    <unit v-if="unitVisabled" msg="SX_V_WORKSHOP" @unit="selUnit" />
  </div>
</template>

<script>
import {
  insertTPrdMo
} from '@/api/production/productionList'
import jumpMateriel from '@/components/JumpMateriel'
export default {
  name: 'ProOrderAdd',
  components: {
    unit: () => import('@/components/unit'),
    material: () => import('@/views/components/material')
    // numberCode: () => import('@/components/NumberCode')
  },
  mixins: [jumpMateriel],
  data() {
    return {
      unitVisabled: false,
      activeName: 'first',
      production: {
        fdate: '',
        fdeliverydate: '',
        fworkshop: '',
        salKhddh: '',
        fdescription: '',
        fcustomer: ''
      },
      fnumber: '',
      isMateria: false,
      aotuTab: false,
      deleteData: [], // 删除用料单数据
      tableHeight: '72vh',
      Entrys: [],
      material: 0,
      tableData: [],
      tableHeard: [
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'fnumber', width: '165px' },
        { label: this.$t('shop.fname2'), type: 'text2', prop: 'fmatname' },
        { label: this.$t('market.fmodel'), type: 'text3', prop: 'fmatmodel' },
        { label: this.$t('shop.fspecificaTion'), type: 'text4', prop: 'fmatspec' },
        { label: this.$t('shop.fdescripTion'), type: 'text6', prop: 'fmatdesc', minWidth: '200' },
        { label: this.$t('market.funit'), type: 'text5', prop: 'fmatunit', width: '100' },
        { label: this.$t('market.fqty'), type: 'tag', prop: 'fqty', width: '100' },
        { label: this.$t('market.workshop'), type: 'states', prop: 'fdept', width: '140' },
        { label: this.$t('market.deliveryDate'), type: 'state', prop: 'fdeliverydate' }
      ]
    }
  },
  created() {
    this.newHang()
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 265px)' : '72vh'
  },
  methods: {
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.material = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 保存
    save() {
      this.tableData.map((res, index) => {
        res.fseq = index + 1
      })
      const DATA = {
        ...this.production,
        details: this.tableData
      }
      insertTPrdMo(DATA).then(res => {
        if (res.code === 0) {
          this.$router.push({
            path: `/proOrderDetails/${res.data}`
          })
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 打开物料弹窗
    handleGetMateriel(row, index) {
      this.isMateria = true
      this.material = index
    },
    // 物料数据
    materialData(item) {
      if (item.fmaterialId) {
        const items = this.tableData[this.material]
        items.fmaterialId = item.fmaterialId
        items.fnumber = item.fnumber
        items.fmatname = item.fname
        items.fdescripTion = item.fdescripTion
        items.fmatspec = item.fspecificaTion
        items.funitId = item.funitId
        items.fmatunit = item.funit
        items.fmatmodel = item.fmodel
      }
      this.isMateria = item.isMaterialDialog
    },
    // 生产部门
    handleWork(index, en) {
      this.worktype = en
      this.indexSelf = index
      this.unitVisabled = true
    },
    // 获取生产车间
    selUnit(ev) {
      if (ev.fid && this.worktype === 2) {
        const item = this.tableData[this.indexSelf]
        item.fdept = ev.fname
        item.fworkshopid = ev.fid
      } else if (ev.fid) {
        this.production.fworkshop = ev.fname
        this.production.fworkshopid = ev.fid
      }
      this.unitVisabled = false
    },
    // 新增行
    newHang() {
      const DATA = {
        fnumber: '',
        fmatmodel: '',
        fdeliverydate: '',
        fdept: '',
        fmatname: '',
        fmatspec: '',
        fdescripTion: '',
        fmatunit: '',
        fmatdesc: '',
        fqty: 0
      }
      this.tableData.push(DATA)
    },
    // 删除行
    delHang() {
      this.tableData.splice(this.indexSelf, 1)
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.indexSelf = row.index
    }
  }
}
</script>
<style lang="scss" scoped>
.proOrderDetails {
  @include listBom;
  .el-tabs {
    .tabPosit {
      padding: 5px;
      box-shadow: 0 0 2px #aaa;
      position: absolute;
      top: 3.5vh;
      left: 0;
      background-color: rgba(255, 255, 255, 1);
      z-index: 99999!important;
    }
    .tabPosit1 {
      padding: 5px;
      box-shadow: 0 0 2px #aaa;
      position: absolute;
      top: -25.69vh;
      left: 0;
      background-color: rgba(255, 255, 255, 1);
      z-index: 9999;
    }
    .el-input::v-deep .el-input__inner {
      height: 30px;
      line-height: 30px;
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        height: 35px;
        width: 275px;
        margin-bottom: 5px;

        .btnType {
          margin-right: 5px;
        }

        .el-date-editor.el-input {
          width: 100% !important;
        }

        .el-select {
          width: 100%;
        }
      }
    }
  }
}
</style>
